// @import
import CmdHandlerFactory from "../handler/CmdHandlerFactory";
import MsgBus from "../comm/network/MsgBus";
import MsgRecognizer from "../comm/network/MsgRecognizer";
import SceneData from "../data/SceneData";

// @import
import mod_gameMsgProtocol = require("../msg/mod_gameMsgProtocol");

// @const
const { ccclass, } = cc._decorator;

/**
 * 登录场景
 */
@ccclass
export default class LoginScene extends cc.Component {
    /**
     * onLoad
     */
    onLoad() {
        SceneData._strCurrSceneName = "LoginScene";

        MsgRecognizer.addProtocol(
            "gameMsgProtocol", mod_gameMsgProtocol
        );

        MsgBus.getInstance().onMsgHandler = (nMsgCode, oMsgBody) => {
            if (nMsgCode < 0 || 
                null == oMsgBody) {
                return;
            }

            let oCmdHandler = CmdHandlerFactory.create(nMsgCode);

            if (null != oCmdHandler) {
                oCmdHandler.handle(oMsgBody);
            }
        }
    }

    /**
     * start
     */
    start(): void {
        cc.find("/Canvas/Compsite_LoginBox/Button_Login").on(cc.Node.EventType.TOUCH_END, () => {
            // 获取用户名称和密码
            let strUserName = cc.find("/Canvas/Compsite_LoginBox/EditBox_UserName")
                .getComponent(cc.EditBox)
                .string;

            let strPassword = cc.find("/Canvas/Compsite_LoginBox/EditBox_Password")
                .getComponent(cc.EditBox)
                .string;

            MsgBus.getInstance().sendMsg(
                mod_gameMsgProtocol.msg.MsgCode.USER_LOGIN_CMD,
                mod_gameMsgProtocol.msg.UserLoginCmd.create({
                    userName: strUserName,
                    password: strPassword,
                })
            );
        });
    }

    /**
     * lateUpdate
     */
    lateUpdate(): void {
        cc.find("/Canvas/Compsite_LoginBox/Button_Login/Sprite_BG").getComponent(cc.Sprite).getMaterial(0).setProperty("currTime", new Date().getMilliseconds());
    }
}
